clear all; close all;

%Some tests for parsing html strings from primetrader
store_data = 'no';
tolerance = 1e-6;

tic; 
data = urlread('http://localhost:8678/quotes/wlist=obxoptions');
lineBreakIndices =  strfind( data, '</tr>');
start_ind = 1;
options = {};
opt_ind = 1;
ivol_vect_ind = 1;

price = 0;
ticker = 'OBX1C330';
type = [ 'put', 'call' ];
T = 0.2;
r = 0.01;
OBX = 390;
ivol_vect = zeros(200,3);
for i = 1:length( lineBreakIndices )
    line = data( start_ind:lineBreakIndices(i)+4 );
    td_start = strfind( line, '<td>' );
    td_end =  strfind( line, '</td>' );
    
    switch store_data
        case 'yes'
            temp = [ str2double( line( td_start(6)+4:td_end(6)-1 ) ) ...
            str2double( line( td_start(10)+4:td_end(10)-1 ) ) ];
            if sum( isnan(temp) ) < 1 && ~isempty(temp) && sum( temp ) > 1e-4
                options{ opt_ind,1 } = line( td_start(5)+4:td_end(5)-1 );
                options{ opt_ind,2 } = temp;
                opt_ind = opt_ind + 1;
            end
        case 'no'
            ticker = line( td_start(5)+4:td_end(5)-1 );
            if length(ticker) > 5 && length(ticker) < 10
                T = getDecimalExpiration( ticker );
                price = (str2double( line( td_start(6)+4:td_end(6)-1 ) ) + ...
                        str2double( line( td_start(10)+4:td_end(10)-1 ) ) )/2;
                strike = str2double( ticker(6:end) );
                if ticker(5) < 'M' && ~isnan(price)
%                     ivol = blsimpv( OBX, strike, r, T, price, 10, 0, tolerance, {type(2)} );
                    ivol = blsImpVolPut( OBX, strike, r, T, price, 0, tolerance );
                else
%                     ivol = blsimpv( OBX, strike, r, T, price, 10, 0, 1e-6, {type(1)} );
                    ivol = blsImpVolCall( OBX, strike, r, T, price, 0, tolerance );
                end
                
                if ~isnan(ivol)
                    ivol_vect( ivol_vect_ind,: ) = [price strike ivol];
                    ivol_vect_ind = ivol_vect_ind+1;
                end
            end
    end
    
    start_ind = lineBreakIndices(i)+5;
end

toc